<?php
/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
include( "../global/initialize.php");

$army_order_id = formatInput( $_GET[ 'id' ] );
$query = "SELECT ruler_id, rank, army_orders.army_id, order_type, name, follow, actual_x, actual_y, no_cancel, complete_turn " .
		"FROM army_orders " .
		"JOIN army ON army.army_id = army_orders.army_id " .
		"WHERE army_order_id = " . $army_order_id;
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$name = $row[ 'name' ];
if( $row[ 'ruler_id' ] == $_SESSION[ 'ruler_id' ] && $row[ 'follow' ] == -1 && $row[ 'no_cancel' ] == 0 ) {
	$army_id = $row[ 'army_id' ];
	$rank = $row[ 'rank' ];
	$type = $row[ 'order_type' ];
	$x = ( int ) round( $row[ 'actual_x' ] );
	$y = ( int ) round( $row[ 'actual_y' ] );
	$completeTurn = $row[ 'complete_turn' ];
	$turn = getCurrentTurn();
	
	if( $type == 1 && $rank == 0 && $completeTurn > 0 ) {
		$query2 = "SELECT start_turn " .
				"FROM ruler " .
				"WHERE ruler_id = " . $_SESSION[ 'ruler_id' ];
		$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
		$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
		$startTurn = $row2[ 'start_turn' ];
		$turn = getCurrentTurn();
		$done = false;
		
		while( !$done ) {
			$query = "SELECT add_turn " .
					"FROM village " .
					"WHERE x = $x AND y = $y";
			$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
			$row = mysql_fetch_array($result, MYSQL_ASSOC);
	
			if( isset( $row[ 'add_turn' ] ) && $row[ 'add_turn' ] + 800 > $turn ) { 
				// the village has been added less then 800 turns ago, we need to check if the ruler has started about the same turn or later
				if( $startTurn + 200 < $row[ 'add_turn' ] ) {
					$x ++;
				}
				else {
					$done = true;
				}
			}
			else {
				$done = true;
			}
		}
		
		$query_update = "UPDATE army " .
				"SET x = $x, y = $y, actual_x = $x, actual_y = $y " .
				"WHERE army_id = " . $army_id;
		mysql_query( $query_update ) or die( "Query failed : " . mysql_error() );
		
		$query_update = "INSERT INTO army_orders " .
				"( `army_id`, `order_type`, `order_value`, `complete_turn`, `order_value2`, `rank`, `start_turn`, `no_cancel` ) " .
				"VALUES( $army_id, 2, 3, " . ( $turn + 3 ) . ", 0, 0, $turn, 1 )";
		//print( "Query: $query_update<br />" );
		mysql_query( $query_update ) or die( "Query failed : " . mysql_error() );
	}
	else {
		$query = "SELECT rank, army_order_id " .
				"FROM army_orders " .
				"WHERE rank > " . $rank . " AND army_id = $army_id";
		$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
		$num = mysql_numrows( $result );
	   $i = 0;
	   while ( $i < $num ) {
			$query = "UPDATE army_orders SET rank = " . ( mysql_result( $result, $i, "rank" ) - 1 ) . " " .
					"WHERE army_order_id = " . mysql_result( $result, $i, "army_order_id" );
			mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	    	$i ++;
	   }
	}
    
	$query_update = "DELETE FROM army_orders " .
			"WHERE army_order_id = " . $army_order_id;
	mysql_query( $query_update ) or die( "Query failed : " . mysql_error() );
	
	$query2 = "SELECT army_id, actual_x, actual_y, ruler_id " .
			"FROM army " .
			"WHERE follow = $army_id";
	$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
	$num2 = mysql_numrows( $result2 );
	$i = 0;
	while ( $i < $num2 ) {
		$query_update = "DELETE FROM army_orders " .
				"WHERE army_id = " . mysql_result( $result2, $i, 'army_id' ) . " AND rank = $rank";
		mysql_query( $query_update ) or die( "Query failed : " . mysql_error() );
		$x = ( int ) round( mysql_result( $result2, $i, 'actual_x' ) );
		$y = ( int ) round( mysql_result( $result2, $i, 'actual_y' ) );
		
		if( $type == 1 && $rank == 0 && $completeTurn > 0 ) {
			$query3 = "SELECT start_turn " .
					"FROM ruler " .
					"WHERE ruler_id = " . mysql_result( $result2, $i, 'ruler_id' );
			$result3 = mysql_query( $query3 ) or die( "Query failed : " . mysql_error() );
			$row3 = mysql_fetch_array( $result3, MYSQL_ASSOC );
			$startTurn = $row3[ 'start_turn' ];
			
			$done = false;
			while( !$done ) {
				$query = "SELECT add_turn " .
						"FROM village " .
						"WHERE x = $x AND y = $y";
				$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
				$row = mysql_fetch_array($result, MYSQL_ASSOC);
		
				if( isset( $row[ 'add_turn' ] ) && $row[ 'add_turn' ] + 800 > $turn ) { 
					// the village has been added less then 800 turns ago, we need to check if the ruler has started about the same turn or later
					if( $startTurn + 200 < $row[ 'add_turn' ] ) {
						$x ++;
					}
					else {
						$done = true;
					}
				}
				else {
					$done = true;
				}
			}
			
			$query_update = "UPDATE army " .
					"SET x = $x, y = $y, actual_x = $x, actual_y = $y " .
					"WHERE army_id = " . mysql_result( $result2, $i, 'army_id' );
			mysql_query( $query_update ) or die( "Query failed : " . mysql_error() );
			$query_update = "INSERT INTO army_orders " .
				"( `army_id`, `order_type`, `order_value`, `complete_turn`, `order_value2`, `rank`, `start_turn`, `no_cancel` ) " .
				"VALUES( " . mysql_result( $result2, $i, 'army_id' ) . ", 2, 3, " . ( $turn + 3 ) . ", 0, 0, $turn, 1 )";
			mysql_query( $query_update ) or die( "Query failed : " . mysql_error() );
		}
		else {
			$query3 = "SELECT rank, army_order_id " .
					"FROM army_orders " .
					"WHERE rank > " . $rank . " AND army_id = " . mysql_result( $result2, $i, 'army_id' );
			$result3 = mysql_query( $query3 ) or die( "Query failed : " . mysql_error() );
			$num3 = mysql_numrows( $result3 );
		   $i3 = 0;
		   while ( $i3 < $num3 ) {
				$query4 = "UPDATE army_orders " .
						"SET rank = " . ( mysql_result( $result3, $i3, "rank" ) - 1 ) . " " .
						"WHERE army_order_id = " . mysql_result( $result3, $i3, "army_order_id" );
				mysql_query( $query4 ) or die( "Query failed : " . mysql_error() );
		   	$i3 ++;
		   }
		}
		$i ++;
	}
}
header( "Location: army.php?name=$name" );
?>